/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.ArrayList;
import java.util.List;
import javabeans.GroupPlan;
import javabeans.Member;
import javabeans.Plan;
import org.hibernate.HibernateException;
import org.hibernate.Query;

/**
 *
 * @author liweijiao
 */
public class IMemberDaoImpl extends DAO implements IMemberDao {

    public Member createMember(Member member) {
        try {
            begin();
            getSession().save(member);
            commit();
            return member;
        } catch (HibernateException e) {
            rollback();
            return null;

        }

    }

    public Member getMember(String email) {
        try {


            Query q = getSession().createQuery("from Member m where m.email = :email");
            q.setString("email", email);
            Member member1 = (Member) q.uniqueResult();

            if (member1 != null) {
                IPlanDaoImpl plandao = new IPlanDaoImpl();
                List<Plan> planlist = plandao.getPlanbyUserId(member1.getId());
                member1.setPersonalplans(planlist);
                System.out.println("member is not null");
                IGroupPlan groupplandao = new IGroupPlanImpl();
                List<GroupPlan> groupplanlist = groupplandao.getGroupPlanbyUserId(member1.getId());
                member1.setGroups(groupplanlist);
                
            }
            return member1;
        } catch (HibernateException e) {
            return null;

        }

    }

    public boolean updateMember(Member member) {
         try {
            begin();
            getSession().saveOrUpdate(member);
            commit();
            return true;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            return false;

        }

    }
}
